//
//  XGRecommendTableViewCell.m
//  Instagram
//
//  Created by monkey on 2019/4/17.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>
#import <SVProgressHUD/SVProgressHUD.h>

#import "XGUserListTableViewCell.h"

@interface XGUserListTableViewCell ()

/// 头像
@property (nonatomic,strong) UIImageView *iconImageView;
/// 名称
@property (nonatomic,strong) UILabel *nameLabel;
/// 描述
@property (nonatomic,strong) UILabel *descriptionLabel;
/// 关注按钮
@property (nonatomic,strong) UIButton *focusButton;

@end

@implementation XGUserListTableViewCell

#pragma mark - 解析数据

- (void)setUser:(AVUser *)user
{
    _user = user;
    
    _nameLabel.text = user[@"screenName"];
    _descriptionLabel.text = user[@"descriptionInfo"];
    
    // 设置头像
    AVFile *file = user[@"userIcon"];
    [XGImageManager imageForAVFile:file placeholderImage:nil imageSize:CGSizeMake(60, 60) backgroundColor:[UIColor whiteColor] isUserIcon:YES completion:^(UIImage * _Nullable image) {
        self.iconImageView.image = image;
    }];
    
    // 设置关注按钮状态
    AVQuery *query = [[XGAccountViewModel currentUser] followeeQuery];
    [query whereKey:@"followee" equalTo:_user];
    [query countObjectsInBackgroundWithBlock:^(NSInteger number, NSError * _Nullable error) {
        self.focusButton.selected = number != 0;
        self.focusButton.backgroundColor = number != 0 ? [UIColor lightGrayColor] : [UIColor redColor];
    }];
    
    // 当前用户隐藏关注按钮
    _focusButton.hidden = [user[@"screenName"] isEqualToString:[XGAccountViewModel currentUser].screenName];
}

#pragma mark - 构造方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
    }
    
    return self;
}

#pragma mark - 事件监听

/// 关注/取消关注事件
- (void)focusOrUnfocusAction:(UIButton *)button
{
    button.selected = !button.isSelected;
    
    if (button.isSelected) {
        // 关注
        button.backgroundColor = [UIColor lightGrayColor];
        [[XGAccountViewModel currentUser] follow:_user.objectId andCallback:^(BOOL succeeded, NSError * _Nullable error) {
            if (error != nil || !succeeded) {
                [SVProgressHUD showErrorWithStatus:@"关注失败,请检查网络连接"];
                return;
            }
            
            [SVProgressHUD showSuccessWithStatus:@"关注成功"];
        }];
    } else {
        // 取消关注
        button.backgroundColor = [UIColor redColor];
        [[XGAccountViewModel currentUser] unfollow:_user.objectId andCallback:^(BOOL succeeded, NSError * _Nullable error) {
            if (error != nil || !succeeded) {
                [SVProgressHUD showErrorWithStatus:@"取消关注失败,请检查网络连接"];
                return;
            }
            
            [SVProgressHUD showSuccessWithStatus:@"取消关注成功"];
        }];
    }
}

#pragma mark - 其他方法

/// 设置界面
- (void)setUpUI
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    // 添加子控件
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.descriptionLabel];
    [self.contentView addSubview:self.focusButton];
    
    //  设置自动布局
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_iconImageView).offset(10);
        make.left.equalTo(self->_iconImageView.mas_right).offset(10);
    }];
    
    [_descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_nameLabel.mas_bottom).offset(10);
        make.left.equalTo(self->_nameLabel);
    }];
    
    [_focusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->_iconImageView);
        make.right.equalTo(self.contentView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
}

#pragma mark - 懒加载

- (UIImageView *)iconImageView
{
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc] init];
    }
    
    return _iconImageView;
}

- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [UILabel labelWithText:@"陈翔六点半" textColor:[UIColor blackColor] font:15 textAlignment:NSTextAlignmentCenter];
    }
    
    return _nameLabel;
}

- (UILabel *)descriptionLabel
{
    if (_descriptionLabel == nil) {
        _descriptionLabel = [UILabel labelWithText:@"陈翔六点半" textColor:[UIColor lightGrayColor] font:12 textAlignment:NSTextAlignmentCenter];
    }
    
    return _descriptionLabel;
}

- (UIButton *)focusButton
{
    if (_focusButton == nil) {
        _focusButton = [UIButton buttonWithTitle:@"关注" fontSize:17 titleColor:[UIColor whiteColor] target:self action:@selector(focusOrUnfocusAction:)];
        [_focusButton setTitle:@"√ 已关注" forState:UIControlStateSelected];
    }
    
    return _focusButton;
}

@end
